package nccloud.mob.riart.message.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import nc.itf.pub.workflowqry.IFlowInstanceQuery;
import nc.itf.uap.pf.IPFWorkflowQry;
import nc.itf.uap.rbac.IUserManageQuery;
import nc.vo.pub.BusinessException;
import nc.vo.pub.workflownote.WorkflownoteAttVO;
import nc.vo.pub.workflowqry.FlowHistoryQryResult;
import nc.vo.sm.UserVO;
import nc.vo.uap.historymsg.HistorymsgVO;
import nccloud.base.exception.ExceptionUtils;
import nccloud.framework.mob.action.itf.IMobileAction;
import nccloud.framework.mob.container.IMobRequest;
import nccloud.framework.service.ServiceLocator;
import nccloud.pubitf.platform.attachment.IAttachmentService;
import nccloud.utils.riart.MessageLogger;
import nccloud.vo.pf.flowinstance.NCCFlowInstanceVO;

/**
 * 获取审批详情信息
 * 
 * @author liyingtj
 *
 */
public class GetApproveDetailAction implements IMobileAction {

	@Override
	public Object doAction(IMobRequest request) {
		String billType = "";
		String billId = "";
		int workflow_type = 0; 

		String jsonStr = request.read();
		JSONObject json2 = JSONObject.parseObject(jsonStr);
		billType = json2.get("billtype").toString();
		billId = json2.get("billid").toString();
		json2.get("workflow_type");
		workflow_type = json2.getIntValue("workflow_type");

		HistorymsgVO[] vos = null;
		try {
			if (json2.get("workflow_type") == null) {
				NCCFlowInstanceVO[] voss = ServiceLocator.find(IFlowInstanceQuery.class)
						.findFlowInstancesByBillIdAndBillTypeAppendOrderByCondition(billId, billType);
				if (voss == null || voss.length == 0) {
					return null;
				} else {
					billId = voss[0].getBillversionpk();
				}

				workflow_type = Integer.valueOf(voss[0].getWorkflow_type());
			}

			FlowHistoryQryResult result = ServiceLocator.find(IPFWorkflowQry.class).queryFlowHistoryQryResult(billType,
					billId, workflow_type);

			IAttachmentService ncservice = ServiceLocator.find(IAttachmentService.class);

			if (result != null && result.getHistorymsgVOs() != null) {

				vos = result.getHistorymsgVOs();

				if (vos != null && vos.length > 0) {

					// 进行附件的增加
					for (HistorymsgVO vo : vos) {
						String[] strs = new String[] { vo.getPk_historymsg() };

						Map<String, List<WorkflownoteAttVO>> mapfiles = ServiceLocator.find(IPFWorkflowQry.class)
								.queryWorkitemAttBatch(strs);

						if (mapfiles.get(vo.getPk_historymsg()) != null
								&& mapfiles.get(vo.getPk_historymsg()).size() > 0) {

							vo.setAttachments(mapfiles.get(vo.getPk_historymsg()).size() + "");
						} else {
							vo.setAttachments(0 + "");
						}

						List<String> userstrs = new ArrayList<String>();
						userstrs.add(vo.getSendman());
						userstrs.add(vo.getDealman());
						UserVO[] userVos = ServiceLocator.find(IUserManageQuery.class)
								.findUserByIDs(userstrs.toArray(new String[0]));
						for (UserVO user : userVos) {
							if (user.getCuserid().equals(vo.getSendman())) {
								vo.setSendman(user.getUser_name());
							} else if (user.getCuserid().equals(vo.getDealman())) {
								vo.setDealman(user.getUser_name());
							}
						}

					}

				} else {
					ExceptionUtils.wrapBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
							.getStrByID("0020nccloud_0", "0ncc0020001-0451")/*
																			 * @res 当前单据没有工作项 ，请刷新重试
																			 */);
					return null;
				}
			} else {
				ExceptionUtils.wrapBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
						.getStrByID("0020nccloud_0", "0ncc0020001-0451")/*
																		 * @res 当前单据没有工作项，请刷新重试
																		 */);
				return null;
			}
			JSONObject retJson = new JSONObject();
			List<JSONObject> rowJsons = new ArrayList<JSONObject>();
			for (int i = 0; i < vos.length; i++) {
//				vos[i].setActivity_content(null);
				vos[i].setAttributeValue("activity_content", null);
				JSONObject jsobj = (JSONObject) JSONObject.toJSON(vos[i]);
				rowJsons.add(jsobj);
			}
			retJson.put("rows", rowJsons);
			return retJson;
		} catch (BusinessException e) {
			MessageLogger.error("操作此类【nccloud.mob.riart.message.actions.GetApproveDetailAction】是出现错误，错误描述：【"+e.getMessage()+"】");
			ExceptionUtils.wrapException(e);
		}
		return null;
	}
}
